<?php
namespace app\home\controller;
use think\Db;

class User extends Common
{
    public function index()
    {
        $data = $this->getLoginUserInfo();
        return $this->fetch('index', ['data' => $data]);
    }

    public function order()
    {
        $userInfo = $this->getLoginUserInfo();

        $sellerIds = '';
        switch ($userInfo['session_role']) {
            case 1:
            case 2:
                $sellers = Db::name('seller')
                    ->field('id')
                    ->where(['agency' => $userInfo['session_id']])
                    ->select();

                $sellerIds = implode(',', array_column($sellers, 'id'));
                break;
            case 3:
                $sellerIds = $userInfo['session_id'];
        }

        $where = [
            'sid' => ['in', $sellerIds],
            'is_delete' => 1
        ];


        $sellerName = input('seller_name');
        if (!empty($sellerName)) {
            $seller = Db::name('seller')
                ->field('id')
                ->where(['name' => $sellerName])
                ->find();

            if ($seller) {
                $where['sid'] = $seller['id'];
            } else {
                $where['sid'] = 0;
            }
        }

        $userName = input('user_name');
        if (!empty($userName)) {
            $user = Db::name('user')
                ->field('id')
                ->where([
                    'name' => $userName,
                ])->find();

            if ($user) {
                $where['uid'] = $user['id'];
            } else {
                $where['uid'] = 0;
            }
        }


        $time = input('time', 1);

        if ($time) {
            switch ($time) {
                case 1:
                    $beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                    $endTime = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
                    $where['create_time'] = ['between', [$beginTime, $endTime]];
                    break;
                case 2:
                    $timestamp = time();
                    $beginTime = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
                    $endTime =strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1;
                    $where['create_time'] = ['between', [$beginTime, $endTime]];
                    break;
                case 3:
                    $beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
                    $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
                    $where['create_time'] = ['between', [$beginTime, $endTime]];
                    break;
            }
        }

        $orders = Db::name('leaseorder')
            ->where($where)->order('create_time', 'desc')
            ->select();

        $orderCount = count($orders);
        $amountCount = array_sum(array_column($orders, 'amount'));
        $realAmountCount = array_sum(array_column($orders, 'real_amount'));

        foreach ($orders as &$item) {
            $item = $this->formatInfo($item);
        }

        return $this->fetch('order', [
            'orders' => $orders,
            'order_count' => $orderCount,
            'amount_count' => $amountCount,
            'real_amount_count' => $realAmountCount,
            'time' => $time,
            'seller_name' => empty($sellerName) ? '' : $sellerName,
            'user_name' => empty($userName) ? '' : $userName,
        ]);
    }

    /**
     * 交易详情
     */
    public function orderDetail()
    {
        $id = input('id');
        $order = Db::name('leaseorder')
            ->where([
                'id' => $id
            ])->find();

        $order = $this->formatInfo($order);

        return $this->fetch('order_detail', [
            'order' => $order
        ]);
    }

    /**
     * 格式化信息
     *
     * @param $item
     * @return mixed
     */
    public function formatInfo($item)
    {
        switch ($item['status']) {
            case 1:
                $item['status'] = '租借中';
                break;
            case 2:
                $item['status'] = '已完成';
                break;
            case 3:
                $item['status'] = '订单超时';
                break;
            case 4:
                $item['status'] = '异常';
                break;
        }

        // 交易用户
        $user = Db::name('user')
            ->field('name')
            ->where([
                'id' => $item['uid'],
            ])->find();

        $item['user_name'] = '';
        if ($user['name']) {
            $item['user_name'] = $user['name'];
        }

        // 商品
        $seller = Db::name('seller')
            ->field('name')
            ->where([
                'id' => $item['sid'],
            ])->find();
        $item['seller_name'] = '';
        if ($seller['name']) {
            $item['seller_name'] = $seller['name'];
        }

        $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
        $item['starttime'] = date('Y-m-d H:i:s', $item['starttime']);
        $item['endtime'] = date('Y-m-d H:i:s', $item['endtime']);

        return $item;
    }
}
